// program/pages/admin/plugins/tencentCloud/customer/list.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentType: 1,
        page: 1,
        search: {
            client_uin: '',
            sales_uin: ''
        },
        salerList: [],
        salerIdx: 0,
        typeList: [{
            value: '',
            label: '全部类型'
        }, {
            value: 'new',
            label: '新拓'
        }, {
            value: 'old',
            label: '存量'
        }, {
            value: 'assign',
            label: '指定'
        }],
        typeIdx: 0,
        customerList: [],
        customerOpenIdx: null,
        reviewCustomerList: []
    },


    bindChangeCurrentType(e) {
        this.setData({
            currentType: e.currentTarget.dataset.current,
        })
        if(this.data.salerList.length>0 && this.data.salerList[0].sales_uin!==1999999999999){
            this.loadData(true)
        }
    },
    bindPickerChange: function (e) {
        this.setData({
            salerIdx: e.detail.value
        })
        this.loadData(true)
    },
    bindTypePickerChange: function (e) {
        this.setData({
            typeIdx: e.detail.value
        })
        this.loadData(true)
    },
    bindSearchInput(e) {
        this.setData({
            ['search.client_uin']: e.detail.value
        })
    },
    bindDoSearch() {
        this.loadData(true);
    },

    bindShowDetail(e) {
        let that = this;
        if (this.data.customerOpenIdx === e.currentTarget.dataset.idx) {
            this.setData({
                customerOpenIdx: null
            })
        } else {
            this.setData({
                customerOpenIdx: e.currentTarget.dataset.idx
            })
            let data = {
                point_url: app.server.adminApi.getTencentCouldCustomerBalance,
                client_uin: this.data.customerList[e.currentTarget.dataset.idx].clientUin
            }
            app.server.getConnect(data).then(function (res) {
                let paramKey1 = 'customerList[' + e.currentTarget.dataset.idx + '].grade'
                let paramKey2 = 'customerList[' + e.currentTarget.dataset.idx + '].balance'
                that.setData({
                    [paramKey1]: res.data.grade || '--',
                    [paramKey2]: res.data.balance
                })
                wx.hideLoading();
            }, function (res) {
                console.log(res);
                wx.hideLoading();
            });
        }
    },
    bindReviewCustomer(e) {
        let status = e.currentTarget.dataset.status === 'agree' ? 1 : 0;
        app.showConfirm('提示', '是否审核' + (status ? '同意' : '拒绝') + '该客户', true, () => {
            let data = {
                point_url: app.server.adminApi.doReviewTencentCouldCustomer,
                client_uin: e.currentTarget.dataset.id
            }
            app.server.getConnect(data).then(function (res) {
                wx.showToast({
                    title: '审核成功',
                })
                that.loadData(true);
                wx.hideLoading();
            }, function (res) {
                console.log(res);
                wx.hideLoading();
            });
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadSalerList();
    },

    loadData(reset) {
        this.loadCustomerList(reset);
    },

    loadSalerList() {
        let that = this;
        let data = {
            point_url: app.server.adminApi.getTencentCouldSalerList
        }
        app.server.getConnect(data).then(function (res) {
            if (res.data.length > 0) {
                if (app.globalData.userInfo.id == 1) {
                    let temp = [{
                        sales_name: '全部业务员',
                        sales_uin: ''
                    }];
                    temp = temp.concat(res.data);
                    that.setData({
                        salerList: temp
                    })
                } else {
                    that.setData({
                        salerList: res.data
                    })
                }
                that.loadData(true);
            } else {
                that.setData({
                    salerList: [{
                        sales_name: '无业务员',
                        sales_uin: 1999999999999
                    }]
                })
            }
            wx.hideLoading();
        }, function (res) {
            console.log(res);
            wx.hideLoading();
        });
    },

    loadCustomerList: function (reset) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        if (reset) {
            this.setData({
                customerList: [],
                reviewCustomerList: [],
                page: 1,
            })
        }
        var that = this;
        var data = {
            point_url: app.server.adminApi.getTencentCouldCustomerList,
            page_no: this.data.page,
            page_size: 20,
            client_uin: this.data.search.client_uin,
            sales_uin: this.data.salerList[this.data.salerIdx].sales_uin,
            type: this.data.currentType,
            client_type: this.data.typeList[this.data.typeIdx].value
        };
        app.server.getConnect(data).then(function (res) {
            if (that.data.currentType === 1) {
                var temp = that.data.customerList;
                temp.push.apply(temp, res.data);
                that.setData({
                    customerList: temp
                })
            } else {
                var temp = that.data.reviewCustomerList;
                temp.push.apply(temp, res.data);
                that.setData({
                    reviewCustomerList: temp
                })
            }
            wx.hideLoading();
        }, function (res) {
            console.log(res);
            wx.hideLoading();
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        app.pageOnloadInit(this, app);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        app.pageOnloadInit(this, app);
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page++;
        this.loadData();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})